package com.lq.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lq.entity.Flight;
import com.lq.mapper.FlightMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

/**
 * @author luoqian
 * @version 1.0
 * @date 2022-04-03 18:48
 */
@CrossOrigin
@RestController
@Api(tags = {"航班管理控制层"})
public class FlightController {

    @Autowired
    private FlightMapper flightMapper;

    @PostMapping("/flight/getPage")
    @ApiOperation(value = "分页获取航班信息", notes = "/flight/getPage")
    public IPage<Flight> getPageStudentInfo(@RequestParam @ApiParam(name = "pageNum", value = "pageNum", required = true) int pageNum, @RequestParam @ApiParam(name = "pageSize", value = "pageSzie", required = true) int pageSize, @RequestBody @ApiParam(name = "flight", value = "flight", required = true) Flight flight) throws Exception {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (flight.getStartTime() != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String start = sdf.format(flight.getStartTime()) + " 00:00:00";
            String end = sdf.format(flight.getStartTime()) + " 23:59:59";
            queryWrapper.between("start_time", sdf1.parse(start), sdf1.parse(end));
        }
        if (!("").equals(flight.getStartCity())) {
            queryWrapper.like("start_city", flight.getStartCity());
        }
        if (!("").equals(flight.getEndCity())) {
            queryWrapper.like("end_city", flight.getEndCity());
        }
        Page<Flight> flightPage = new Page<>(pageNum, pageSize);
        return flightMapper.selectPage(flightPage, queryWrapper);
    }

    @PostMapping("/flight/insert")
    @ApiOperation(value = "新增航班信息", notes = "/flight/insert")
    public int insert(@RequestBody @ApiParam(name = "route", value = "route", required = true) Flight flight) {
        return flightMapper.insert(flight);
    }

    @PostMapping("/flight/update")
    @ApiOperation(value = "修改航班信息", notes = "/flight/update")
    public int update(@RequestBody @ApiParam(name = "route", value = "route", required = true) Flight flight) {
        return flightMapper.updateById(flight);
    }

    @PostMapping("/flight/delete")
    @ApiOperation(value = "批量删除航班信息", notes = "/flight/delete")
    public int delete(@RequestParam @ApiParam(name = "ids", value = "1,2,3", required = true) String ids) {
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        return flightMapper.deleteBatchIds(list);
    }
}
